﻿Imports System
Imports System.IO
Imports System.Diagnostics
Public Class FolderService

    Private fswCarpetas As FileSystemWatcher
    Protected Sub New()
        fswCarpetas = New FileSystemWatcher()
        fswCarpetas.Path = My.Settings.Carpeta
        Dim result As WaitForChangedResult = fswCarpetas.WaitForChanged(WatcherChangeTypes.Created)

        If result.Name.Contains("finnish-it") Then
        
            FileSystem.RmDir(My.Settings.Carpeta & "\" & result.Name)

            Dim compresor As New Process
            compresor.StartInfo = New ProcessStartInfo(My.Application.Info.DirectoryPath & "\winrar.exe")
            If Not EventLog.SourceExists("compresor", ".") Then
                EventLog.CreateEventSource("compresor", "killerbytes service")
            End If
            With compresor.StartInfo
                .Arguments = "x subida.rar " & My.Settings.Carpeta
                .CreateNoWindow = True
                .UseShellExecute = False
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .WorkingDirectory = My.Computer.FileSystem.GetParentPath(My.Settings.Carpeta)

            End With
            Try
                compresor.Start()
            Catch ex As Exception

            End Try
            SyncLock compresor
                WriteLog("Descomprimiendo")
            End SyncLock
            WriteLog("Terminado")
        End If

    End Sub

    Protected Sub Cerrar()
        fswCarpetas.EnableRaisingEvents = False
    End Sub

    Private Sub WriteLog(ByVal mensaje As String)
        Dim ELog As New EventLog("killerbytes service", ".", "compresor")
        ELog.WriteEntry(mensaje)
        ELog.WriteEntry(mensaje, EventLogEntryType.Information, 234, CType(3, Short))
    End Sub




End Class
